package net.sx.minemodule.mvp.activities;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bean.WXPayBean;
import com.evnet.PaySucEvent;
import com.homegoods.aroutercomment.ARouterPath;
import com.kedll.library.KBaseActivity;
import com.kedll.library.base.MyBaseFragmentActivity;
import com.kedll.library.listener.OnClickListener;
import com.kedll.library.mvp.BasePresenter;
import com.kedll.library.util.RxBus;
import com.kedll.library.util.ToastUtil;
import com.pay.PaySDKHelp;
import com.trello.rxlifecycle2.android.ActivityEvent;

import net.sx.minemodule.R;
import net.sx.minemodule.databinding.MineActivityOrderPayBinding;
import net.sx.minemodule.mvp.presenters.MineOrderPayPresenter;
import net.sx.minemodule.mvp.views.MineOrderPayView;

import java.util.ArrayList;
import java.util.List;

/**
 * 订单支付界面
 *
 * @author 11470
 */
@Route(path = ARouterPath.PATH_MINE_ORDER_PAY)
public class MineOrderPayActivity extends MyBaseFragmentActivity<MineActivityOrderPayBinding, MineOrderPayPresenter> implements MineOrderPayView {
    private String content;
    private String addreSid;
    private String price;
    private String payType;
    private List<String> orderSids;
    private int type;
    private PaySDKHelp sdkHelp;

    @SuppressLint("CheckResult")
    @Override
    protected void initEvent() {
        //初始化支付
        sdkHelp = new PaySDKHelp(MineOrderPayActivity.this);
        mBinding.ivOneBottomPay.setOnClickListener(listener);
        mBinding.ivTwoBottomPay.setOnClickListener(listener);
        mBinding.ivThreeBottomPay.setOnClickListener(listener);
        mBinding.ivFourBottomPay.setOnClickListener(listener);
        RxBus.getDefault().toFlowable(PaySucEvent.class)
                .compose(this.bindUntilEvent(ActivityEvent.DESTROY))
                .subscribe(loginSucEvent -> {
                    //支付成功回调
                    ToastUtil.showToast("支付成功");
                });

        onClicked(mBinding.payBuy, o -> {
            if (type == 0) {
                mPresenter.orderPay(orderSids, payType);
            } else {
                mPresenter.pay(payType, content, addreSid);

            }

        });
        mBinding.ivOneBottomPay.performClick();

    }

    public View.OnClickListener listener = v -> {
        int i = v.getId();
        if (i == R.id.iv_one_bottom_pay) {
            payType = "balance";
            mBinding.payBuy.setText("余额支付" + price + "元");
            mBinding.ivOneBottomPay.setSelected(true);
            mBinding.ivTwoBottomPay.setSelected(false);
            mBinding.ivThreeBottomPay.setSelected(false);
            mBinding.ivFourBottomPay.setSelected(false);
        } else if (i == R.id.iv_two_bottom_pay) {
            payType = "weixin";
            mBinding.payBuy.setText("微信支付" + price + "元");
            mBinding.ivOneBottomPay.setSelected(false);
            mBinding.ivTwoBottomPay.setSelected(true);
            mBinding.ivThreeBottomPay.setSelected(false);
            mBinding.ivFourBottomPay.setSelected(false);
        } else if (i == R.id.iv_three_bottom_pay) {
            payType = "alipay";
            mBinding.payBuy.setText("支付宝支付" + price + "元");
            mBinding.ivOneBottomPay.setSelected(false);
            mBinding.ivTwoBottomPay.setSelected(false);
            mBinding.ivThreeBottomPay.setSelected(true);
            mBinding.ivFourBottomPay.setSelected(false);
        } else {
            payType = "card";
            mBinding.payBuy.setText("银行卡支付" + price + "元");
            mBinding.ivOneBottomPay.setSelected(false);
            mBinding.ivTwoBottomPay.setSelected(false);
            mBinding.ivThreeBottomPay.setSelected(false);
            mBinding.ivFourBottomPay.setSelected(true);
        }
    };

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        if (getIntent() != null) {
            type = getIntent().getIntExtra("type", 0);
            if (type == 0) {
                orderSids = new ArrayList<>();
                orderSids.add(getIntent().getStringExtra("orderSid"));
            } else {
                addreSid = getIntent().getStringExtra("addreSid");
                content = getIntent().getStringExtra("content");
            }
            price = getIntent().getStringExtra("price");
            mBinding.tvPayMoneyOk.setText(price);
        }

    }

    @Override
    protected void initToolBar(Bundle savedInstanceState) {
        super.initToolBar(savedInstanceState);
        setTitle("确认付款");
    }

    @Override
    protected MineOrderPayPresenter getPresenter() {
        return new MineOrderPayPresenter(this);
    }

    @Override
    protected int getLayout() {
        return R.layout.mine_activity_order_pay;
    }

    @Override
    public void doActivityPay(WXPayBean wxPayBean,String payType) {
        sdkHelp.pay(wxPayBean,payType);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        sdkHelp.onDestroy();
    }
}
